define(function (require, exports, module) {
	var $ = require("jquery2");
	require("layui");
	
	var utils = require("utils");
	var ctx = utils.constants.ctx;
	var demoService = require("bean/demoService");
	
	

	var layerObject = null;

	
	var config = {
			requestUrl     : '/demo/getDemoList',//表单请求的url
			tableEle       : '#LAY_table_demo',//表单元素
			showBoxFilter  :"demo",//前端弹出框显示数据的from的filter	
	}

	var demoList = {
			init:function(){
				this.showTable();
				this.initListener();
			},
			//初始化表格
			showTable:function(){
					  
				//方法级渲染表格
				objectTable  = layui.table.render({
					    elem: config.tableEle,
					    url: config.requestUrl,
					    where: { timestemp: new Date() },
					    cols: [[
					      TheField
					      {field:'', 
					    	 title: '操作',
					    	 width:'20%',
					    	 templet:function(data){
					    	  
					    	  var doEdit = '<button data="'+data.thePrimaryKey+'"  class="doEdit layui-btn layui-btn-xs">修改</button>';
					    	  var doDel = '<button data="'+data.thePrimaryKey+'" class="doDel layui-btn layui-btn-danger layui-btn-xs">删除</button>';
					    	  
					          return doEdit+doDel;
					        }
					      }
					    ]]
					    ,id: 'objectTable'
					    ,page: true
					  });
				
					//搜索
					var $ = layui.$, active = {
						    reload: function(){
						      //执行重载
						      layui.table.reload('objectTable', {
						        page: {
						          curr: 1 //重新从第 1 页开始
						        }
						        ,where: {
						        	  searchText: $('#theSearchText').val()
						        }
						      });
						    }
						  };
						  
						  $('.layui-btn').on('click', function(){
							    var type = $(this).data('type');
							    active[type] ? active[type].call(this) : '';
						  });	
					  
			},
			initListener:function(){//初始化监听事件
				var self = this;
				
			
				
				//提交修改监听(会把数据存到data里面)
				layui.form.on('submit(edit)', function(data){
					self.doEditOrUpdate(data.field);
					return false; //阻止表单跳转（不可以写在方法里面）。如果需要表单跳转，去掉这段即可。
				});
				
				
				//删除
				$(document).on("click",".doDel",function(){
					var id = $(this).attr("data");
					self.doDel(id);
				});
				
				
				//添加
				$(document).on("click","#add",function(){
					self.showChildBox();
				})
				
				
				//修改
				$(document).on('click','.doEdit',function(){
					var childId = $(this).attr("data");
					self.showChildBox(childId);
				})
				
			},
			doEditOrUpdate:function(data){
				try {
					demoService.saveOrUpdata(data);
					layer.msg("修改成功");
				} catch (e) {
					layer.msg("修改失败");
					console.log(e);
				}
				
				//重新加载
				objectTable.reload('objectTable', {
					  url: config.requestUrl
				});
				layer.close(childBox);
			},
			showChildBox:function(produceId){
				  $("#thePrimaryKey").val("");//置空主键
				  var title ="";
				  if(produceId==null){//新增
					  title = "demoBoxTitleAdd";
				  }else{
					  title = "demoBoxTitleEdit";
					  var msg = demoService.getDemo(produceId);
				  }
				
				 childBox = layui.layer.open({
					  type: 1,
					  title:title,
					  content:$("#child").html(),
					  area: ['840px', '600px'],
					  maxmin: true,
					  success: function(layero, index){
						  
theTimeCode
						  if(msg!=null){
							layui.form.val(config.showBoxFilter, {
theAttrCode
							})
						  }
			
							layui.form.render();//重新渲染
							layerObject = layero;
					  },
					  end:function(){
						  $(".search").hide();
					  }
				});
				 layui.layer.full(childBox);
			},
			doDel:function(id){
				 layui.layer.msg('确定删除吗？', {
					  time: 0 //不自动关闭
					  ,btn: ['删除', '取消']
					  ,yes: function(index){
						  layer.close(index);
						  try{
							  demoService.delDemo(id);
							  layer.msg("删除成功");
						  }catch(e){
							  layer.msg("删除失败,请联系系统管理员。");
							  console.log(e);
						  }
						//重新加载
						objectTable.reload('objectTable', {
							  url: config.requestUrl
						});
					    
					  }
					});
			}
			
			
	}
	demoList.init();
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

});